import { defineComponent, ref, h, DefineComponent, inject, } from "vue";
import { NCard, NButton, NSpace, } from "naive-ui"
import Maincontainer from "@/components/maincontainer/index"
import Flexform, { flexformitem } from "@/components/flexform/index"
import useTableIndex from "@/hooks/tableindex"
import useHttpMessage from "@/hooks/oftenattr"
import useColums from "@/hooks/colums"
import Flextalbe from "@/components/flextable/index"
import Mypage from "@/components/mypage/index"
export default defineComponent({
    name: "workflat",
    setup() {
        const {
            formValue,
            loading,
            total,
            tableData,
            editRef,
            pageRef,
            formRef,
            handleSearch,
            handleResetForm,
            handleView,
            handleEdit,
            handleCreate,
            refreshDelete
        } = useTableIndex()
        const { http, message } = useHttpMessage()
        // 自定义属性区域
        const overDeleteFunc: Function | undefined = inject("overDeleteFunc")
        const handleDelete = (data: any, index: number) => {
            const params = { "id": data.id }
            overDeleteFunc && overDeleteFunc(params, http.zidiandelete, refreshDelete, "字典")
        }
        const valueRef = ref<DefineComponent | null>(null)
        const formList = ref([
            {
                span: 6,
                label: "名称",
                key: "name",
                default: "",
                type: "input",
                placeholder: "请输入名称",
            },
            { slot: (item: flexformitem) => <div class="w-full"></div>, span: 6, type: "slot" },
            { slot: (item: flexformitem) => <div class="w-full"></div>, span: 6, type: "slot" },
            {
                slot: (item: flexformitem) => <div class="w-full mt-3px">
                    <NButton size={"small"} onClick={handleSearch}>搜索</NButton><NButton size={"small"} onClick={handleResetForm}>重置</NButton><NButton size={"small"} onClick={handleCreate}>新建</NButton></div>, span: 6, type: "slot"
            }
        ])
        const { index, createdTime, changeTime, desc, name, } = useColums()
        const showDictValue = (data: any, index: number) => {
            console.log("点击查看", index, editRef.value);
            valueRef.value && valueRef.value.handleInit(data)
        }
        const colnums = ref([
            index,
            name,
            desc,
            createdTime,
            changeTime,
            {
                title: "字典值",
                width: 100,
                render: (data: any, index: number) => {
                    return h(NSpace, {}, {
                        default() {
                            return [
                                h(NButton, { size: "small", onClick: () => showDictValue(data, index), text: true, type: "info" }, { default() { return "字典值" } }),
                            ]
                        }
                    })
                }
            },
            {
                title: "操作",
                width: 200,
                render: (data: any, index: number) => {
                    return h(NSpace, {}, {
                        default() {
                            return [
                                h(NButton, { size: "small", onClick: () => handleView(data), type: "primary" }, { default() { return "查看" } }),
                                h(NButton, { size: "small", onClick: () => handleEdit(data), type: "info" }, { default() { return "修改" } }),
                                h(NButton, { size: "small", onClick: () => handleDelete(data, index), type: "error" }, { default() { return "删除" } }),
                            ]
                        }
                    })
                }
            },
        ])

        const getTableDataFunc = (page: number, pageSize: number) => {
            const params = { page, pageSize, ...formValue.value }
            http.zidianlist(params).then((res: any) => {
                if (res.data.code === 0) {
                    tableData.value = res.data.items
                    total.value = res.data.total
                } else {
                    message.error(res.msg || "获取字典数据列表失败")
                }
            }).catch((err: Error) => {
                message.error(err.message || "系统错误");
            })
        }

        // 初始化函数
        const init = () => {
            pageRef.value && pageRef.value.changePage()
        }
        init()
        return () => (
            <Maincontainer>
                workflat
            </Maincontainer>
        )
    },
})
